package com.example.module_motocircle.helper

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.PopupWindow
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.example.lib_common.adapter.CommonVDBRecyclerAdapter
import com.example.lib_common.dp
import com.example.module_motocircle.R
import com.example.module_motocircle.databinding.ItemPopupMenuRowBinding
import com.example.module_motocircle.databinding.LayoutPopupNavigationBinding
import com.example.module_motocircle.model.Model


class SeriesPopupMenuRvHelper(private val layoutInflater: LayoutInflater, private val onSelect: ((String, Model) -> Unit)?) {
    private var mAdapter: CommonVDBRecyclerAdapter<Model, ItemPopupMenuRowBinding>? = null
    private var mItems = ArrayList<Model>()

    private var mNavigationId: String = ""
    fun bindItems(id: String, list: List<Model>) {
        this.mNavigationId = id
//        list.last().last = true
        mItems.clear()
        mItems.addAll(list)
    }

    private val mBinding by lazy {
        LayoutPopupNavigationBinding.inflate(layoutInflater)
    }

    private val mPopupWindow by lazy {
        PopupWindow().apply {
            width = ViewGroup.LayoutParams.WRAP_CONTENT
            height = ViewGroup.LayoutParams.WRAP_CONTENT
            isFocusable = true
            isOutsideTouchable = true
            contentView = mBinding.root
        }
    }

    fun showAgain(view: View) {
        this.showAsDropDown(view)
    }

    private fun showAsDropDown(v: View) {
        mPopupWindow.showAsDropDown(v, -(50.dp - v.width / 2), 10.dp)
    }

    fun show(view: View): SeriesPopupMenuRvHelper {
        this.showAsDropDown(view)
        initAdapter()
        return this
    }

    private fun initAdapter() {
        if (mAdapter == null) {
            mAdapter = object : CommonVDBRecyclerAdapter<Model, ItemPopupMenuRowBinding>(R.layout.item_popup_menu_row, mItems) {
                override fun convert(holder: BaseDataBindingHolder<ItemPopupMenuRowBinding>, item: Model) {
                    holder.dataBinding?.apply {
//                        val commonPopupMenu = Model(name = item.name, checked = item.checked, last = item.last, tag = item.modelId)
                        holder.dataBinding?.item = item
                        holder.dataBinding?.tvMenu?.isSelected = item.checked
                        holder.dataBinding?.executePendingBindings()//刷新UI
                    }
                }
            }
            mBinding.rvPopupMenu.adapter = mAdapter
            mAdapter!!.setOnItemClickListener { adapter, _, position ->
                val target = mItems[position]
                target.checked = true
                adapter.notifyDataSetChanged()
                onSelect?.invoke(mNavigationId, target)
                this@SeriesPopupMenuRvHelper.mPopupWindow.dismiss()
            }
        } else {
            mAdapter?.notifyDataSetChanged()
        }
    }
}